var rtpth=location.href.match(/(http:\/\/.*?\/)index\.php/)[1];

var imdismod=false;
var imdloadedmm=false;
var imdunsel='<img src="http://img42.imageshack.us/img42/2183/unselected1oa.gif" />';
var imdsel='<img src="http://img299.imageshack.us/img299/4107/selected6fi.gif" />';
var imdtform='<select class="forminput" name="a" onchange="imdforumc(this)"><option value="close">Close Topics</option>'+
            '<option value="open">Open Topics</option>'+
            '<option value="pin">Pin Topics</option>'+
            '<option value="unpin">Unpin Topics</option>'+
            '<option value="delete">Delete Topics</option>'+
            '<option value="clear">Clear Selected Topics</option>'+
            '</select> '+
            '<input name="submit" type="button" class="forminput submit" onclick="imdforumact(this.form)" value="With Selected" />';
var imdttstore=[];
var _forumid=0;

function createXMLRequest()
{
if(window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
return new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
return false;
}
}
}
}

function encq(d)
{
var nd='';
for(var i=0;i<d.length;i++)
{
var cc=d.charCodeAt(i);
if(cc>127)
nd+='&#'+cc+';';
else 
nd+=d.charAt(i)
}
return encodeURIComponent?encodeURIComponent(nd):escape(nd)
}


/*from quirksmode*/
function cokc(name,value,days)
{
	if(days)
	{
		var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires="; expires="+date.toGMTString();
	}
	else var expires="";
	document.cookie=name+"="+value+expires+"; path=/";
}

function cokr(name)
{
	var nameEQ=name+"=";
	var ca=document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while(c.charAt(0)==' ')c=c.substring(1,c.length);
		if(c.indexOf(nameEQ)==0)return c.substring(nameEQ.length,c.length);
	}
	return null;
}

var imdtsel=cokr('imdtsel');
if(imdtsel)imdtsel=imdtsel.split(',');
else imdtsel=[];

function imdtissel(tid)
{
    for(t in imdtsel)
    if(imdtsel[t].split(':')[1]==tid)return true;
    return false
}

function imdttoggle(tid,but)
{
    imdtsel=cokr('imdtsel');
    if(imdtsel)imdtsel=imdtsel.split(',');
    else imdtsel=[];
    
    var fid=_forumid;
    if(!imdtissel(tid))
    {
        imdtsel=imdtsel.concat(fid+':'+tid);
        but.innerHTML=imdsel;
    }
    else
    {
        var n=[];
        for(t in imdtsel)
            if(imdtsel[t].split(':')[1]!=tid)n[n.length]=imdtsel[t];
        imdtsel=n;
        but.innerHTML=imdunsel;
    }
    cokc('imdtsel',imdtsel.join(','));
    imdforumgui()
}

function imdforumc(o)
{
}

function imdforumq(lst,act)
{
    var itm=lst.pop();
    if(!itm)
    {
        cokc('imdtsel','',-1);
        window.location.reload();
        return
    }
    
    var fid=itm[0];
    
    var tidq = '';
    for(i in itm[1])
    tidq+='TID_'+itm[1][i]+'=1&';
    
    var thd=createXMLRequest();
    thd.open('POST',rtpth+'index.php?act=modcp&f='+fid+'&CODE=topicchoice&tact='+act,true);
    thd.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    thd.onreadystatechange = function(){if(thd.readyState==4){
        if(thd.responseText.indexOf('Control Panel Message')!=-1)
        {
            var err=thd.responseText.match(/Control Panel Message<\/div>(\n|.)*<div class='tablepad'>(.+?)<\/div>/)[2];
            alert('An error occurred while performing inline moderation with the forum ID '+fid+':\n\n'+err)
        }
        imdforumq(lst,act)
    }};
    thd.send(tidq);
}

function imdforumact(frm)
{
    if(frm.a.value=='clear')
    {
        cokc('imdtsel','',-1);
        window.location.reload();
        return
    }
    var queue=[];
    for(t in imdtsel)
    {
        var fid=imdtsel[t].split(':')[0];
        var tid=imdtsel[t].split(':')[1];
        if(!queue[fid])queue[fid]=[];
        queue[fid][queue[fid].length]=tid
    }
    if(queue.length==0)
    {
        alert('No topics were selected for moderation.');
        return
    }
    var nq=[];
    for(i in queue)
    {
        nq[nq.length]=[i,queue[i]]
    }
    frm.submit.value="Please wait...";
    frm.submit.className="forminput submit working";
    imdforumq(nq,frm.a.value)
}

function imdforumgui()
{
    gui=document.getElementById('imdfgui');
    if(!gui)
    {
        var ff=null;
        var f=document.forms;
        for(var i=0;i<f.length;i++)
        {
            if(!f[i].action)continue;
            if(f[i].action.match(/act=SF$/))
            {
                ff=f[i];
                break
            }
        }
        
        var gui=document.createElement('form');
        gui.className='imdfgui';
        gui.id="imdfgui";
        ff.parentNode.insertBefore(gui,ff.nextSibling);
    
        gui.innerHTML=imdtform;
    }
    
    gui.submit.value='With Selected ('+imdtsel.length+')'
}

function imdforumetttype(f,evt,tid,desc)
{
	if(evt.keyCode)k=evt.keyCode;
	else if(evt.which)k=evt.which;
    if(k==13)
    {
        if(f.value=='')
        {
            return false;
        }
        
        f.className = 'forminput imdtt imdtt-field-working';
        
        var fid=_forumid;
        var thd=createXMLRequest();
        thd.open('GET',rtpth+'index.php?showtopic='+tid,true);
        thd.onreadystatechange = function(){if(thd.readyState==4){
            var m=thd.responseText.match(/auth_key' value='(.+?)'/);
            if(m)
            {
                var auth=m[1];
                var ithd=createXMLRequest();
                ithd.open('POST',rtpth+'index.php?auth_key='+auth+'&f='+fid+'&t='+tid+'&CODE=12&TopicTitle='+encq(f.value)+'&TopicDesc='+encq(desc),true);
                ithd.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                ithd.onreadystatechange = function(){if(ithd.readyState==4){
                    if(ithd.responseText.indexOf('Please wait while we transfer you')!=-1)
                    {
                        var te=f.parentNode;
                        var c=imdttstore[tid];
                        c=c.replace(/class=['"]?desc['"]?>.+?<\/span>/i,"class='desc'>"+desc+"</span>");
                        f.parentNode.innerHTML=c;
                        var link=te.getElementsByTagName('a')[0];
                        link=link.href.match(/getnewpost/)?te.getElementsByTagName('a')[1]:link;
                        link.innerHTML=f.value;
                        imdttstore[tid]='';
                    }
                    else
                    {
                        f.className = 'forminput imdtt';
                        alert('An error occurred while changing the topic title. No change was made.');
                        window.location.reload()
                    }
                }};
                ithd.send('act=Mod');
            }
            else
            {
                f.className = 'forminput imdtt';
                f.parentNode.innerHTML=imdttstore[tid];
                imdttstore[tid]='';
                alert('You do not have sufficient moderating abilities.');
                window.location.reload()
            }
        }};
        thd.send(null);
        return false
    }
    else if(k==27)
    {
        f.parentNode.innerHTML=imdttstore[tid];
        imdttstore[tid]='';
        return false
    }
    return true
}

function imdforumett(evt)
{
	tid=evt['target']?evt['target']['tid']:evt['srcElement']['tid'];
    var te=document.getElementById('te'+tid);
    imdttstore[tid]=te.innerHTML;
    var link=te.getElementsByTagName('a')[0];
    link=link.href.match(/getnewpost/)?te.getElementsByTagName('a')[1]:link;
    try{var desc=link.parentNode.innerHTML.match(/<span class=["']?desc["']?>(.+?)<\/span>/i)[1];}catch(e){var desc=''}
    te.innerHTML='<input type="text" name="title" class="forminput imdtt" value="'+link.innerHTML+'" onkeydown="return imdforumetttype(this,event,'+tid+',\''+desc.replace(/(['"])/,'\\\1')+'\')" class="imdtt" />';
    te.childNodes[te.childNodes[0].nodeType!=1?1:0].select();
    te.childNodes[te.childNodes[0].nodeType!=1?1:0].focus();
}

function imdforum()
{
    if(document.body.innerHTML.indexOf('No topics were found. This is either because there are')!=-1)return;
    var fth=document.getElementsByTagName('th');
    for(var i=0;i<fth.length;i++)
    {
        if(fth[i].innerHTML.match(/Last Action/))
        {
            var te=document.createElement('th');
            te.className='titlemedium';
            fth[i].parentNode.appendChild(te);
            break;
        }
    }
    
    var fth=document.getElementsByTagName('td');
    for(var i=0;i<fth.length;i++)
    {
        if(!fth[i].style.padding)continue;
        if(parseInt(fth[i].style.padding)!=6)continue;
        if(fth[i].innerHTML.match(/(Important|Forum) Topics/i))
        {
            fth[i].setAttribute('colspan',6);
            if(fth[i].getAttribute('colspan')==5)
            {
                var te=document.createElement('td');
                te.className='darkrow1';
                fth[i].parentNode.appendChild(te);
            }
        }
    }
    
    var ftl=document.links;
    for(var i=0;i<ftl.length;i++)
    {
        var tm=ftl[i].href.match(/showtopic=([0-9]+)$/);
        if(tm)
        {
            var tid=parseInt(tm[1]);
            var te=document.createElement('td');
            te.className='row2';
            te.innerHTML='<span style="cursor:pointer" onclick="imdttoggle('+tid+',this)">'+(imdtissel(tid)?imdsel:imdunsel)+'</span>';
            if(ftl[i].parentNode.nodeName.toLowerCase()=='b')
                var utd=ftl[i].parentNode.parentNode;
            else utd=ftl[i].parentNode;
            utd.parentNode.appendChild(te);
            
            if(!window.opera)
            {
                utd.tid=tid;
                utd.id="te"+tid;
                if(window.addEventListener)utd.addEventListener('dblclick',imdforumett,false);
                else utd.attachEvent('ondblclick',imdforumett);
            }
        }
    }
    
    imdforumgui()
}

function imdsetup()
{
    if(!createXMLRequest)return;
    if(!document.getElementById)return;
    if(!document.body.innerHTML)return;
    var lnks=document.links;
    for(var i=0;i<lnks.length;i++)
        if(lnks[i].href.match(/act=modcp/)){imdismod=true;break;}
    if(!imdismod)return;
    
    if(window.location.href.match(/showforum=|act=SF/))
    {
        if(window.location.href.match(/showforum=/))
        {
            _forumid=window.location.href.match(/showforum=([0-9]+)/)[1];
        }
        else
        {
            _forumid=window.location.href.match(/f=([0-9]+)/)[1];
        }
        imdforum()
    }
}